package edu.gatech.ealf.ettplaf;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.accessibility.Accessible;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.Timer;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicArrowButton;

public class ETTComboBoxUI extends ComboBoxUI implements MouseListener, ExtendedToolTipProvider {

    private ETTModel model;
    private Timer toolTipTimer = null;
    private Timer extendedToolTipTimer = null;
    
    public ETTComboBoxUI(JComponent c) {
        super();
        model = new ETTModel(c, this);
    }

    public static ComponentUI createUI(JComponent c) {
        if(c instanceof BasicArrowButton){
            return null;
        }
        return new ETTComboBoxUI(c);
    }
    

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        ExtendedToolTipLookAndFeel.getInstance().registerComponentInstallation(this, c);
        c.addMouseListener(this);
    }

    public void paint(Graphics g, JComponent c) {
    }

    @Override
    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        c.removeMouseListener(this);
        ExtendedToolTipLookAndFeel.getInstance().registerComponentUninstallation(this, c);
    }

    @Override
    public void update(Graphics g, JComponent c) {
    }

    public void mouseClicked(MouseEvent e) {
        
    }


    public void mouseEntered(MouseEvent e) {
        if(model.glass == null){
            model.setGlass((Container) model.component.getRootPane().getGlassPane());
        }
        model.lazyLoad();
        toolTipTimer = new Timer(ExtendedToolTipLookAndFeel.toolTipDelayMs, model.toolTipHandler);
        extendedToolTipTimer = new Timer(ExtendedToolTipLookAndFeel.extendedToolTipDelayMs, model.extendedToolTipHandler);
        toolTipTimer.start();
        extendedToolTipTimer.start();
    }

    public void mouseExited(MouseEvent e) {
        toolTipTimer.stop();
        extendedToolTipTimer.stop();
        ETTHelper.getInstance().requestRemoveToolTip();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void stopExtendedToolTipTimer() {
        extendedToolTipTimer.stop();
    }

    public void stopToolTipTimer() {
        toolTipTimer.stop();
    }

    @Override
    public boolean isFocusTraversable(JComboBox c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isPopupVisible(JComboBox c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void setPopupVisible(JComboBox c, boolean v) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public Accessible getAccessibleChild(JComponent c, int i) {
        return null;
    }
}

